package com.keke.user.reglog.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.keke.user.reglog.service.UserService;
import com.keke.utils.SystemException;

@WebServlet("/api/ChangeInfo")
public class ChangeInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static UserService service = new UserService();

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		return;
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String data = (String) req.getAttribute("data");
		PrintWriter out = resp.getWriter();
		String info = null;
		try {
			info = service.changeInfo(data);
			out.print(" {\"isOk\":true,"
		       		+ "\"data\":\""+info+"\"}");
		} catch (SystemException e) {
			info = e.toString().split(": ")[1];
			out.print(" {\"isOk\":false,"
		       		+ "\"data\":\""+info+"\"}");
		} catch (Exception e) {
			out.print(" {\"isOk\":false,"
		       		+ "\"data\":\"ERROR\"}");
		}
	}
}